package net.skystudio.es.common;

import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Getter;
import lombok.Setter;
import org.apache.http.HttpStatus;
import org.springframework.data.domain.Page;

/**
 * @author nicky
 * @Description: 抽象控制器
 * @date 2019-04-16
 **/
public abstract class AbstractController {

    @Setter
    @Getter
    @ApiModel(value = "响应结果备注")
    public static class ResponseResult<T> {

        @ApiModelProperty(value = "请求响应状态标识 SUCCESS-成功 FAILURE-失败")
        private ResponseResult.ResponseState state;

        @ApiModelProperty(value = "请求返响应状态码 200-成功 404-资源找不到 ...")
        private int code;

        @ApiModelProperty(value = "描述信息")
        private String message;

        @ApiModelProperty(value = "返回泛型数据")
        private T data;

        public enum ResponseState {
            SUCCESS,
            FAILURE
        }

    }

    protected <T> ResponseResult<T> buildResponseResult() {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setCode(HttpStatus.SC_OK);
        responseResult.setState(ResponseResult.ResponseState.SUCCESS);
        return responseResult;
    }

    protected <T> ResponseResult<T> buildResponseResult(T data) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setData(data);
        responseResult.setCode(HttpStatus.SC_OK);
        responseResult.setState(ResponseResult.ResponseState.SUCCESS);
        return responseResult;
    }

    protected PageBean convertPageToPageBean(Page page) {
        if(page == null){
            return null;
        }
        PageBean pageBean = new PageBean();
        pageBean.setPageSize(page.getSize());
        pageBean.setParameters(page.getTotalElements());
        pageBean.setPageNum(page.getNumber() + 1);
        pageBean.setDatas(page.getContent());
        return pageBean;
    }

    protected <T> ResponseResult<T> buildResponseResult(String message) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setMessage(message);
        responseResult.setCode(HttpStatus.SC_OK);
        responseResult.setState(ResponseResult.ResponseState.SUCCESS);
        return responseResult;
    }

    protected <T> ResponseResult<T> buildResponseResult(T data, String message) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setData(data);
        responseResult.setMessage(message);
        responseResult.setCode(HttpStatus.SC_OK);
        responseResult.setState(ResponseResult.ResponseState.SUCCESS);
        return responseResult;
    }

    protected <T> ResponseResult<T> buildResponseResult(String message, int code, ResponseResult.ResponseState state) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setMessage(message);
        responseResult.setCode(code);
        responseResult.setState(state);
        return responseResult;
    }

    protected <T> ResponseResult<T> buildResponseResult(T data, String message, int code,
                                                        ResponseResult.ResponseState state) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setData(data);
        responseResult.setMessage(message);
        responseResult.setCode(code);
        responseResult.setState(state);
        return responseResult;
    }

    protected <T> ResponseResult<T> buildResponseResult(String message, ResponseResult.ResponseState state) {
        ResponseResult<T> responseResult = new ResponseResult<>();
        responseResult.setMessage(message);
        responseResult.setCode(HttpStatus.SC_OK);
        responseResult.setState(state);
        return responseResult;
    }

}
